﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using Utility;

namespace Quanly
{
    public partial class ThemNguoiDung : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        #region Methods

        private MsgReturn AddUser()
        {
            var fullname = txtFullName.Value.Trim();
            var phone = txtPhone.Value.Trim();
            var email = txtEmail.Value.Trim();
            var username = txtUserName.Value.Trim();
            var extra = txtExtra.Value.Trim();
            List<int> rights = new List<int>();
            if (chboxUserMng.Checked) rights.Add(Constant.MANAGER_USER);
            if (chboxBillMng.Checked) rights.Add(Constant.MANAGER_BILL);
            if (chboxBillTotalMng.Checked) rights.Add(Constant.MANAGER_BILLTOTAL);
            var user = new QLBH_User
            {
                FullName = fullname,
                Phone = phone,
                Email = email,
                UserName = username,
                ExtraInfo = extra,
                PassWord = Md5Password.EncryptPwd(Constant.PWD_DEFAULT),
                LastLogin = DateTime.Parse("2014-01-01"),
                Status = 1
            };
            return WebProvider.Provider.User.AddUser(user, rights);
        }

        private void NoticeMsg(string msg)
        {
            ClientScript.RegisterStartupScript(typeof(Page), "SymbolAlert",string.Format("<script type='text/javascript'>alert('{0}');</script>",msg));
        }

        #endregion

        #region Events
        protected void btnSave_Click(object sender, EventArgs e)
        {
            var msgReturn = AddUser();
            NoticeMsg(msgReturn.Message);
        }

        protected void btnSaveClose_Click(object sender, EventArgs e)
        {
            var msgReturn = AddUser();
            if (msgReturn.IsSuccess)
            {
                Response.Redirect(Constant.UserMng_Path);
            }
            else
            {
                NoticeMsg(msgReturn.Message);
            }
        }

        #endregion
       
    }
}